<?php
class Kdtauthapi extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('kdt_api_model');
    }

    //手动同步有赞订单
    public function updateTrades()
    {
        if (!is_cli()) die('request forbidden');
        $this->load->model('kdt_push_model');

        $startdate = "2017-01-01 00:00:00";
        $start = strtotime($startdate);
        $enddate = "2018-04-27 00:00:00";
        $end = strtotime($enddate);

        $step=24*3600*80;
        $stop=$start+$step;
        $dur=$end-$start;
        $loop=$dur<$step?1:ceil($dur/$step);

        $echoindex = 1;

        while (true){
            if ($loop == 0||$loop < 0) break;

            $startstr = date('Y-m-d H:i:s',$start);
            $stopstr = date('Y-m-d H:i:s',$stop);
            $loopstr = (string)$loop;
            echo "loop: ".$loopstr." ".$startstr." ".$stopstr.PHP_EOL;
            echo "*************************************************************************".PHP_EOL;
            $pageno = 1;
            while (true){
                $res=$this->kdt_api_model->getTradesSold($startstr,$stopstr,$pageno);
                if ($res===false) die('kdt api error');

                $this->kdt_push_model->updateTradesOrdersConsigns($res['trades'],$echoindex);

                if ($res["has_next"]===false) break;

                $echoindex+=20;
                $pageno++;
            }

            $loop--;
            $start+=$step;
            $stop +=$step;
        }
    }

    public function getGoodsInfo()
    {
        if (!is_cli()) die('request forbidden');
        $this->kdt_api_model->getGoodsInfo();
    }

    public function getSalesmanCustomers()
    {
        $pagesize=$this->input->get('pagesize');
        $pagesize=!isset($pagesize)||!is_numeric($pagesize)?20:$pagesize;
        $pageno=$this->input->get('page');
        $pageno=!isset($pageno)||!is_numeric($pageno)?1:$pageno;
        $mobile=$this->input->get('mobile');
        if (!isset($mobile)||!is_numeric($mobile)) die('request forbidden');

        $res=$this->kdt_api_model->getSalesmanCustomers($pagesize,$pageno,$mobile);
        echo '<pre>';
        var_dump($res);
        echo '</pre>';
    }

    public function getSalesmanAccount()
    {
        $mobile=$this->input->get('mobile');
        if (!isset($mobile)||!is_numeric($mobile)) die('request forbidden');

        $res=$this->kdt_api_model->getSalesmanAccount($mobile);
        echo '<pre>';
        var_dump($res);
        echo '</pre>';
    }

    public function getUserInfo()
    {
        $openid=$this->input->get('openid');
        $fansid=$this->input->get('fansid');

        $res=$this->kdt_api_model->getUserInfo($openid,$fansid);
        echo '<pre>';
        var_dump($res);
        echo '</pre>';
    }

    public function authcallback()
    {
        $this->kdt_api_model->authcallback();
        echo '授权成功';
    }

    public function getGoodsOnSale($pageno)
    {
        $res=$this->kdt_api_model->getGoodsOnSale($pageno);
        foreach ($res as $row){
            echo $row['title'].'&nbsp;'.$row['item_id'].'<br>';
        }
    }

}